
<template>
  <div>
    <Card ref="card">
      <p slot="title">
        {{
          issaved == 0
            ? "日间照料中心等级评定汇总"
            : "日间照料中心等级评定报告"
        }}- {{ groupname }}
      </p>
      <Row :gutter="16">
        <Col span="4">
          <Select
            v-model="personState"
            size="large"
            placeholder="所有日间照料中心"
          >
            <Option
              v-for="item in stateList"
              :value="item.value"
              :key="item.value"
              >{{ item.label }}</Option
            >
          </Select>
        </Col>
        <Col span="4">
          <Input v-model="pName" size="large" placeholder="名称" />
        </Col>

        <Col span="12">
          <Button @click="Search" size="large" type="primary" icon="ios-search"
            >查询</Button
          >
        </Col>

        <Col span="2">
          <Button
            @click="Export"
            size="large"
            type="primary"
            :loading="exportLoading"
            icon="md-download"
            >导出</Button
          >
        </Col>
      </Row>
    </Card>
    <iframe
      id="iframe-print"
      frameborder="0"
      style="display: none"
      scrolling="auto"
      :src="printSrc"
    ></iframe>
    <Modal :mask-closable="false" v-model="modal2" width="500">
      <p slot="header" class="color-primary text-align-center">
        <Icon type="ios-information-circle"></Icon>
        <span>修改机构信息</span>
      </p>
      <Form :model="personTemp" :label-width="80">
        <FormItem label="名称">
          <Input type="text" v-model="personTemp.Name" size="large" />
        </FormItem>

        <FormItem label="联系人">
          <Input v-model="personTemp.LinkName" size="large" />
        </FormItem>
        <FormItem label="联系方式">
          <Input v-model="personTemp.LinkPhone" size="large" />
        </FormItem>
        <FormItem label="地址">
          <Input v-model="personTemp.Address" size="large" />
        </FormItem>
      </Form>

      <div slot="footer">
        <Button type="primary" size="large" v-on:click="SaveAssessRijian"
          >保存</Button
        >
      </div>
    </Modal>

    <Modal :mask-closable="false" v-model="modal3" width="500">
      <p slot="header" class="color-primary text-align-center">
        <Icon type="ios-information-circle"></Icon>
        <span>转移到项目</span>
      </p>
      <br />
      <Row>
        <Select
          filterable
          label-in-value
          v-model="tempgroup"
          size="large"
          @on-change="perChange"
          placeholder="请选择项目"
        >
          <Option v-for="item in grouplist" :value="item.Gid" :key="item.Gid">{{
            item.GroupName
          }}</Option>
        </Select>
      </Row>
      <br />
      <Row>
        <Input v-model="temptown" size="large" placeholder="所属乡镇" />
      </Row>
      <br />
      <Button
        size="large"
        @click="temptown = item.Town"
        v-bind:key="item.Town"
        v-for="item in townList"
        style="margin-left: 5px; margin-bottom: 5px"
        >{{ item.Town }}</Button
      >

      <br />
      <div slot="footer">
        <Button type="primary" size="large" v-on:click="TransRijianGroup"
          >保存</Button
        >
      </div>
    </Modal>

    <Row class="margin-top-10">
      <Table
        border
        :columns="tableTitle"
        :data="tableData"
        ref="table"
        :height="tableHeight"
      ></Table>
    </Row>
    <Row class="margin-top-10">
      <Page
        :total="total"
        :current="pPage"
        :page-size="pSize"
        :page-size-opts="[50, 100, 300, 500, 1000, 2000, 5000]"
        @on-change="
          (s) => {
            pPage = s;
            Search();
          }
        "
        @on-page-size-change="
          (p) => {
            pSize = p;
            pPage = 1;
            Search();
          }
        "
        show-total
        show-sizer
      />
    </Row>
  </div>
</template>
<script>
import dayjs from "dayjs";
import excel from "@/libs/excel";
import { Message, Modal } from "view-design";
import { printUrl } from "@/libs/util";
import {
  SearchAssessRijian,
  DeleteAssessRijian,
  SaveAssessRijian,
  SearchRijianGrouphasPage,
  TransRijianGroup,
  GetRijianTown,
} from "@/api/rijian";
export default {
  name: "search_rijian",
  data() {
    return {
      tableHeight: 450,
      total: 0,
      pPage: 1,
      pSize: 50,
      exportLoading: false,

      groupname: "",
      isNew: false,
      modal2: false,
      modal3: false,
      modalRemark: false,
      pringtTitle: "",
      personTemp: {},
      tableTitle: [],
      tableData: [],
      stateList: [
        {
          label: "所有日间照料中心",
          value: "-1",
        },
        {
          label: "待评定",
          value: "0",
        },
        {
          label: "达标",
          value: "1",
        },
        {
          label: "未达标",
          value: "2",
        },
      ],
      personState: "-1",
      pName: "",
      town: "",
      townList: [],
      tempid: "",
      grouplist: [],
      tempgroup: "",
      temptown: "",
    };
  },
  props: {
    id: {
      type: String,
      required: true,
    },
    issaved: {
      type: Number,
      required: true,
    },
  },
  watch: {
    personState: function (newQuestion, oldQuestion) {
      this.Search();
    },
  },
  methods: {
    perChange(e) {
      this.tempgroup = e.value;
      this.GetRijianTown(this.tempgroup);
    },
    handleEdit(row) {
      if (row.$isEdit) {
        this.$set(row, "$isEdit", false);
      } else {
        this.$set(row, "$isEdit", true);
      }
    },
    handleSave(row, index) {
      if (row.$isEdit) {
        this.$set(row, "$isEdit", false);
      } else {
        this.$set(row, "$isEdit", true);
      }
    },
    SaveAssessRijian() {
      let vm = this;
      if (!this.personTemp.Name) {
        Message.error("名称不能为空");
        return;
      }

      SaveAssessRijian(this.personTemp).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.modal2 = false;
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    SearchRijianGrouphasPage() {
      let vm = this;
      var reqData = {
        issaved: 0,
      };
      SearchRijianGrouphasPage(reqData).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.grouplist = data.Data.Rows;
        } else {
          vm.$Message.error(data.Message);
        }
      });
    },
    DeleteAssessRijian(id) {
      this.$Modal.confirm({
        title: "提示",
        content: "确定要删除此日间照料中心信息吗？",
        onOk: () => {
          DeleteAssessRijian(id).then((res) => {
            const data = res.data;
            if (data.Type == 1) {
              this.Search();
              this.$Message.success("删除成功");
            } else {
              this.$Modal.error({ title: "提示", content: data.Message });
            }
          });
        },
        onCancel: () => {},
      });
    },
    GetRijianTown(groupid) {
      let vm = this;
      GetRijianTown(groupid).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.townList = data.Data;
        } else {
        }
      });
    },
    TransRijianGroup() {
      let vm = this;
      if (!vm.tempgroup) {
        this.$Modal.error({ title: "提示", content: "请选择项目" });
        return;
      }
      var reqData = {
        gid: this.tempid,
        groupid: this.tempgroup,
        town: this.temptown,
      };
      TransRijianGroup(reqData).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.modal3 = false;
          this.Search();
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    Search() {
      let vm = this;
      var isnew = 0;
      if (vm.isNew) {
        isnew = 1;
      }
      var town = vm.town;
      if (town == "所有乡镇") {
        town = "";
      }
      var reqData = {
        groupid: vm.id,
        state: vm.personState,
        isnew: isnew,
        page: vm.pPage,
        size: vm.pSize,
        str: vm.pName,
      };
      SearchAssessRijian(reqData).then((res) => {
        const data = res.data;
        if (data.Type == 1) {
          vm.total = data.Data.Total;
          vm.tableData = data.Data.Rows;
        } else {
          Message.error(data.Message);
        }
      });
    },
    Export() {
      if (this.tableData.length) {
        this.exportLoading = true;
        var exportData = [];
        this.tableData.forEach((e) => {
          exportData.push({
            R_Index: e.R_Index,
            AreaName: e.AreaName,
            Name: e.Name,
            Address: e.Address, 
            AssessLevel: e.AssessLevel + "星级", 
            CheckTime: e.CheckTime ? dayjs(e.CheckTime).format("MM月DD日") : "",
           State: e.State ==1 ? "达标" : e.State ==2 ? "不达标":"", 
            Remark: e.Remark,
          });
        });
        this.exportLoading = true;
        const params = {
          title: [
            "序号",
            "县区",
            "名称",
            "地址", 
            "申请等级", 
            "评定时间",
            "评定结果", 
            "备注",
          ],
          key: [
            "R_INDEX",
            "AreaName",
            "Name", 
            "Address",
            "AssessLevel", 
            "CheckTime",
            "State", 
            "Remark",
          ],
          data: exportData,
          autoWidth: true,
          filename: this.groupname,
        };
        excel.export_array_to_excel(params);
        this.exportLoading = false;
      } else {
        this.$Message.info("表格数据不能为空！");
      }
    },
  },
  created() {},
  mounted() {
    var cardHeight = this.$refs.card.$el.offsetHeight;
    this.tableHeight = window.innerHeight - cardHeight - 200;
    this.groupname = this.$route.query.groupname;
    if (this.issaved == 0) {
      this.tableTitle = [
        {
          title: "序号",
          key: "R_Index",
          align: "center",
          width: 80,
        },
        {
          title: "操作",
          key: "action",
          width: 180,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.tempid = params.row.Gid;
                      this.modal3 = true;
                    },
                  },
                },
                "转移到项目"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small",
                  },
                  on: {
                    click: () => {
                      this.DeleteAssessRijian(params.row.Gid);
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
        {
          title: "机构名称",
          key: "Name",
          align: "center",
          width: 200,
        },

        {
          title: "联系人",
          key: "LinkName",
          align: "center",
          width: 120,
        },
        {
          title: "联系方式",
          key: "LinkPhone",
          align: "center",
          width: 200,
        },
        {
          title: "地址",
          key: "Address",
          minWidth: 200,
        },
        {
          title: "状态",
          align: "center",
          width: 120,
          render: (h, rparams) => {
            if (rparams.row.State == 0) {
              return h(
                "Tag",
                {
                  props: {
                    color: "default",
                  },
                },
                "待评定"
              );
            } else {
              var ctype = "default";

              return h(
                "Button",
                {
                  props: {
                    type: "success",
                    size: "small",
                  },
                  on: {
                    click: () => {
                      this.$router.push({
                        name: "search_rijian_info",
                        params: { id: rparams.row.Gid, name: rparams.row.Name },
                      });
                    },
                  },
                },
                "已评定"
              );
            }
          },
        },
      ];
    } else {
      this.tableTitle = [
        {
          title: "序号",
          key: "R_Index",
          align: "center",
          width: 80,
        },
        {
          title: "机构名称",
          key: "Name",
          align: "center",
          width: 200,
        },

        {
          title: "联系人",
          key: "LinkName",
          align: "center",
          width: 120,
        },
        {
          title: "联系方式",
          key: "LinkPhone",
          align: "center",
          width: 200,
        },
        {
          title: "地址",
          key: "Address",
          minWidth: 200,
        },
        {
          title: "状态",
          align: "center",
          width: 120,
          render: (h, rparams) => {
            if (rparams.row.State == 0) {
              return h(
                "Tag",
                {
                  props: {
                    color: "default",
                  },
                },
                "待评定"
              );
            } else {
              var ctype = "default";

              return h(
                "Button",
                {
                  props: {
                    type: "success",
                    size: "small",
                  },
                  on: {
                    click: () => {
                      this.$router.push({
                        name: "search_rijian_info",
                        params: { id: rparams.row.Gid, name: rparams.row.Name },
                      });
                    },
                  },
                },
                "已评定"
              );
            }
          },
        },
      ];
    }

    this.SearchRijianGrouphasPage();
    this.Search();
  },
};
</script>
